package cate.game.role.show.msg;

import cate.common.table.d.GDBlog;
import cate.game.GameBody;
import cate.game.chat.ChatRequest;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBBlog;
import cate.game.event.master.RoleNameEvent;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.util.api.ConsoleApi;
import cp.solution.business.game.role.NameRegEntity;
import cp.solution.business.sys.env.EnvironmentVarSets;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcException;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

@ClientMsg(title = "修改名字 の 请求")
public class ModifyNameReq implements MsgInput {

    @NoteField("名字")
    public String name;

    @NoteField(value = "名字内容验证")
    public String checkSign;

    @Override
    public void execute(GameBody game, ClientContext cc) {
       EcResult r =  settle(cc.getRole());
       if(r.ok())
           game.eventDispatcher.dispatchEvent(new RoleNameEvent(cc.getRole()));
       game.notice.message(cc.getRole(), r, true);
    }

    private EcResult settle(Role role) {
        EcResult r = new EcResult();
        if(EnvironmentVarSets.getInstance().isRenameDisabled()) {
            return r.fail(EnvironmentVarSets.getInstance().getRenameDisabledAlert());
        }
        this.name = StringUtils.trim(name);
        this.name = name.replace(" ", "");
        if (StringUtils.equals(role.getBase().name, name)) {
            return r.fail("名字不能相同");
        }
        if (!role.getGame().tools.sensitiveWords.legal(name)) {
            return r.fail("名字包含敏感词汇");
        }
        if (name.length() < GDBlog.NAME_LENGTH_MIN || name.length() > GDBlog.NAME_LENGTH_MAX) {
            return r.fail("名字长度请在"+GDBlog.NAME_LENGTH_MIN+"-"+GDBlog.NAME_LENGTH_MAX+"个字符之间");
        }
        try {
            ChatRequest.checkSign(this.name, checkSign);
        } catch (EcException e) {
            return r.fail("fail on checkSign."+e.message);
        }
//        try {
//            EcResult rApi = ChatApi.checkRoleName(role, this.name);
//            if(!rApi.ok()) {
//                return r.fail(rApi.message);
//            }
//        } catch (Exception e) {
//            return r.fail("fail on call yzx api:"+e.getMessage());
//        }
        MixRes cost = null;
        if (role.getSystem().modifyNameTime >= GDBlog.FREE_MODIFY_NAME_TIME) {
            //优先消耗道具
            cost = new MixRes(GDBlog.MODIFY_NAME_ITEM_COST_STR);
            r = cost.consumeCheck(role);
            if (!r.ok()) {
                //道具不足消耗碎钻
                cost = new MixRes(GDBlog.MODIFY_NAME_COST_STR);
                r = cost.consumeCheck(role);
                if (!r.ok()) {
                    //碎钻都没有？
                    return r;
                }
            }
        }
        try {
            ConsoleApi.regNameAndSet(role.getGame(), role, name, NameRegEntity.REG_TYPE_RENAME);
        } catch (Exception e) {
            return r.fail("名字已重复");
        }
        if (cost != null) {
            cost.consumeDirect(role);
        }
        role.getBase().name = name;
        role.getSystem().modifyNameTime++;
        role.sendNow(new ModifyNameResp(role));
        return r.success();
    }

    @Override
    public MsgConfig conf() {
        return MsgIdBBlog.MODIFY_NAME_REQ;
    }
}
